package starlingui.component.page
{
	import com.imcrl.component.page.Page;
	
	import starlingui.vo.IconVO;
	
	public class AdvPage extends Page
	{
		private var list:Vector.<IconVO>;
		public function AdvPage(pageSize:uint)
		{
			super(pageSize);
		}
		
		
		/**
		 *  插入数据; (其它项向后移,如果此页中有空格,就占用空格,如果没有,会把最后一项推到下一页中)
		 * @param pageIndex
		 * @param index
		 * @param data 插入的数据(支持空数据);
		 * 
		 */		
		public function insert(pageIndex:int,index:int,newData:*):Boolean{
			if(index>pageSize){
				return false;
			}
			var pageFirstIndex:int=pageIndex*pageSize;
			var pageLastIndex:int=pageFirstIndex+pageSize-1;
			index=pageFirstIndex+index;
			
			var i:int;
			/**
			 * 插入的位置是否前面还有空位,有空位就移动空位;
			 */
			for(i=pageFirstIndex;i<index;i++){
				if(list[i]==null){
					index=i;
					break;
				}
			}
			
			var oldData:IconVO=list[index];
			if(oldData==newData){
				return false;
			}
			
			/**
			 * 如果旧数据为空,说明此项可直接放入数据;
			 */
			if(oldData==null){
				list[index]=newData;
				return true;
			}
			
			/**
			*如果插入项有数据,看看本页是否有空格,删除一个空格来放数据;
			*/
			for(i=index;i<pageLastIndex;i++){
				oldData=list[i];
				if(oldData==null)list.splice(i,1);
			}
			list.splice(index,0,newData);
			
			this.invalidate();
			return true;
		}
		
		
		/**
		 * 替换某一项数据 
		 * @param pageIndex
		 * @param index
		 * @param newData
		 * @return 
		 * 
		 */		
		public function repleaceData(pageIndex:int,index:int,newData:IconVO):Boolean{
			if(index>pageSize){
				return false;
			}
			index=pageIndex*pageSize+index;
			
			list[index]=newData;
			return true;
		}
		
		
		/**
		 * 删除其中一项 
		 * @param pageIndex
		 * @param index
		 * @return 
		 * 
		 */		
		public function remove(pageIndex:int,index:int):Boolean{
			if(index>pageSize){
				return false;
			}
			var pageFirstIndex:int=pageIndex*pageSize;
			var newLastIndex:int=pageFirstIndex+pageSize;//-1;
			index=pageFirstIndex+index;
		
			//添加一个项到本页最后一个，删除一个项;
			if(newLastIndex>=dataLength-1){
				list.push(null);
			}else{
				list.splice(newLastIndex,0,null);
			}
			list.splice(index,1);
			
			return true;
		}
		
		
		public function changeSlot(pageIndex:int,dragSlot:int,dropSlot:int):void{
			if(dragSlot==dropSlot || pageIndex>totalPage)return;
			
			dragSlot=pageIndex*pageSize+dragSlot;
			dropSlot=pageIndex*pageSize+dropSlot;
			
			var dragData:IconVO=list[dragSlot];
			var dropData:IconVO=list[dropSlot];
			
			list[dragSlot]=dropData;
			list[dropSlot]=dragData;
		}
		
		
		/**
		 * 二级菜单调换位置; 
		 * @param pageIndex
		 * @param dragSlot
		 * @param dropSlot
		 * 
		 */		
		public function subListChangeSlot(sub:Vector.<IconVO>,dragSlot:int,dropSlot:int):void{
			var len:int=sub.length;
			if(dragSlot>=len || dropSlot>=len)return;
			var dragData:IconVO=sub[dragSlot];
			var dropData:IconVO=sub[dropSlot];
			
			sub[dragSlot]=dropData;
			sub[dropSlot]=dragData;
		}
		
		
		public function get length():int{
			return this.totalPage;
		}
		
		
		override public function set data(value:*):void{
			this.list=value;
			super.data=value;
		}
	}
}